package com.yzt.minemodule.fragment;

import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.yzt.zhuju.RXingUtils;
import com.yzt.zhuju.base.BaseFragment;
import com.yzt.zhuju.network.entity.mine.UserLevelBaseBean;
import com.yzt.zhuju.utils.SpannableFactory;
import com.yzt.zhuju.widget.ZzHorizontalProgressBar;
import com.yzt.minemodule.R;
import com.yzt.minemodule.adapter.UserLevelAdapter;
import com.yzt.plugin_widget.RoundImageView;

/**
 * author : bob
 * date   : 2023/1/16
 * desc   : 财富等级
 */
public class UserWealthLevelFragment extends BaseFragment {

    private UserLevelAdapter userLevelAdapter;

    private RoundImageView imgHeader;

    private TextView tvLevel;

    private TextView tvLevelTo;

    private ImageView imgIcon;

    private String extraUserId;

    private FrameLayout flUserContainer;

    private ZzHorizontalProgressBar progressBar;

    private UserLevelBaseBean baseBean;

    public static UserWealthLevelFragment getInstance(String extraUserId, UserLevelBaseBean baseBean) {
        Bundle args = new Bundle();
        args.putString("extra_user_id", extraUserId);
        args.putParcelable("extra_data", baseBean);
        UserWealthLevelFragment levelFragment = new UserWealthLevelFragment();
        levelFragment.setArguments(args);
        return levelFragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_user_wealth_level;
    }

    @Override
    public void installViews() {
        RecyclerView rvRoomList = findViewById(R.id.recycler_view);
        userLevelAdapter = new UserLevelAdapter();
        userLevelAdapter.setType(0);
        rvRoomList.setLayoutManager(new LinearLayoutManager(mActivity));
        rvRoomList.setAdapter(userLevelAdapter);

        flUserContainer = findViewById(R.id.fl_user_container);
        imgHeader = findViewById(R.id.img_header);
        tvLevel = findViewById(R.id.tv_level);
        tvLevelTo = findViewById(R.id.tv_level_to);
        imgIcon = findViewById(R.id.img_icon);
        progressBar = findViewById(R.id.progress);

        animator = ObjectAnimator.ofFloat(imgIcon, "rotationY", -360f, 0f);
        animator.setDuration(4000);
        animator.setInterpolator(new LinearInterpolator());
        animator.setRepeatCount(-1);
        animator.start();

        extraUserId = getArguments().getString("extra_user_id");
        baseBean = getArguments().getParcelable("extra_data");

        loadPageData();

        // 用户Id不等于空则表示看的不是自己，那么将隐藏部分布局逻辑
        if (extraUserId != null) {
            flUserContainer.setVisibility(View.GONE);
        }
    }

    private void loadPageData() {
        String head = RXingUtils.getPersonDetail().getUserBasic().getAvatar();
        Glide.with(this).load(head).into(imgHeader);
        tvLevel.setText("LV." + baseBean.getRiches().getGrade());
        int progress = (int)((baseBean.getRiches().getRankExperience() - baseBean.getRiches().getExperienceRequired()) / Float.parseFloat(""+baseBean.getRiches().getRankExperience())* 100);
        progressBar.setProgress(progress);
        tvLevelTo.setText(SpannableFactory.create(baseBean.getRiches().getExperienceRequired() + "").bold().append(" riches to level up").build());
        //Glide.with(this).load(UserLevelUtils.getWealthBigIcon(getContext(), baseBean.getRiches().getGrade())).into(imgIcon);
        Glide.with(this).load(baseBean.getRiches().getIcon()).into(imgIcon);
        userLevelAdapter.addData(baseBean.getRichesConfig());
    }

    @Override
    public void netWorkRequest() {

    }

    private ObjectAnimator animator;

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (animator != null) {
            animator.cancel();
            animator = null;
        }
    }
}
